﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Model;
namespace Thep 
{
    public partial class FrmDacTrungVatLieu : Form
    {
        public FrmDacTrungVatLieu()
        {
            InitializeComponent();
        }

        #region Database
        OleDbConnection conn;
        OleDbCommand cmd;

        public void Connect()
        {
            string connectstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ Application.StartupPath + "\\Database.a;Persist Security Info=False";
            conn = new OleDbConnection(connectstring);
            cmd = conn.CreateCommand();
        }
        #endregion

        private string MacThepID = "";

        private string MacThepSanID = "";

        private string MacBeTongID = "";

        private string MacBuLongID = "";

        #region Methods

        #region Mac thep ket cau

        public void ActiveButton1(bool them, bool luu, bool xoa, bool huy)
        {
            btnThem1.Enabled = them;
            btnLuu1.Enabled = luu;
            btnXoa1.Enabled = xoa;
            btnHuy1.Enabled = huy;

        }

        public void CleanText1()
        {
            txtTenMacThep.Text = "";
            txtBungDam.Text = "";
            txtCanhDam.Text = "";
            txtBungCot.Text = "";
            txtCanhCot.Text = "";
            txtSuonGoi.Text = "";
            txtTamDem.Text = "";
            txtModuleThepKetCau.Text = "";
        }

        public void ActiveText1(bool flag)
        {
            txtTenMacThep.Enabled = flag;
            txtBungDam.Enabled = flag;
            txtCanhDam.Enabled = flag;
            txtBungCot.Enabled = flag;
            txtCanhCot.Enabled = flag;
            txtSuonGoi.Enabled = flag;
            txtTamDem.Enabled = flag;
            txtModuleThepKetCau.Enabled = flag;
            grcMacThep.Enabled = !flag;
        }

        public DataTable LoadMacThep()
        {
            Connect();
            DataTable loaithep = new DataTable();
            loaithep.Columns.Add("MacThepKetCauID", typeof(int));
            loaithep.Columns.Add("TenMacThep", typeof(string));
            try
            {
                cmd.CommandText = "SELECT * FROM MacThepKetCau";
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    DataRow dr = loaithep.NewRow();
                    dr["MacThepKetCauID"] = int.Parse(reader["MacThepKetCauID"].ToString());
                    dr["TenMacThep"] = reader["TenMacThep"].ToString();
                    loaithep.Rows.Add(dr);
                }
                return loaithep;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        public void InsertMacThep(MacThepKetCau thep)
        {
            try
            {
                cmd.CommandText = "INSERT INTO MacThepKetCau(TenMacThep,BungDam,CanhDam,BungCot,CanhCot,SuonGoi,TamDem,ModuleDanHoiThepKetCau) VALUES('" + thep.TenMacThep + "','" + thep.BungDam + "','" + thep.CanhDam + "','" + thep.BungCot + "','" + thep.CanhCot + "','" + thep.SuonGoi + "','" + thep.TamDem + "','" + thep.ModuleDanHoiThepKetCau + "')";
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public MacThepKetCau FindMacThep(int t)
        {
            try
            {
                Connect();
                MacThepKetCau thep = new MacThepKetCau();
                cmd.CommandText = "SELECT * FROM MacThepKetCau WHERE MacThepKetCauID=" + t;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    thep.MacThepKetCauID = int.Parse(reader["MacThepKetCauID"].ToString());
                    thep.TenMacThep = reader["TenMacThep"].ToString();
                    thep.BungDam = float.Parse(reader["BungDam"].ToString());
                    thep.CanhDam = float.Parse(reader["CanhDam"].ToString());
                    thep.BungCot = float.Parse(reader["BungCot"].ToString());
                    thep.CanhCot = float.Parse(reader["CanhCot"].ToString());
                    thep.SuonGoi = float.Parse(reader["SuonGoi"].ToString());
                    thep.TamDem = float.Parse(reader["TamDem"].ToString());
                    thep.ModuleDanHoiThepKetCau = float.Parse(reader["ModuleDanHoiThepKetCau"].ToString());
                }


                return thep;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }


        public void DeleteMacThep(int id)
        {
            try
            {
                cmd.CommandText = "DELETE FROM MacThepKetCau WHERE MacThepKetCauID = " + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void Delete(MacThepKetCau m)
        {

            try
            {
                cmd.CommandText = "DELETE FROM KetQua WHERE MacThepKetCauID = " + m.MacThepKetCauID;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        public KetQua FindKetQua(MacThepKetCau m)
        {
            try
            {
                Connect();
                KetQua ketqua = new KetQua();
                cmd.CommandText = "SELECT * FROM KetQua WHERE MacThepKetCauID = " + m.MacThepKetCauID;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ketqua.KetQuaID = int.Parse(reader["KetQuaID"].ToString());
                }
                return ketqua;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        #endregion

        #region Mac be tong

        public void ActiveButton2(bool them, bool luu, bool xoa, bool huy)
        {
            btnThem2.Enabled = them;
            btnLuu2.Enabled = luu;
            btnXoa2.Enabled = xoa;
            btnHuy2.Enabled = huy;
        }

        public void CleanText2()
        {
            txtTenMacBeTong.Text = "";
            txtBeTongSan.Text = "";
            txtBeTongCot.Text = "";
            txtModuleBeTongCot.Text = "";
            txtModuleBeTongSan.Text = "";
        }

        public void ActiveText2(bool flag)
        {
            txtTenMacBeTong.Enabled = flag;
            txtBeTongSan.Enabled = flag;
            txtBeTongCot.Enabled = flag;
            txtModuleBeTongCot.Enabled = flag;
            txtModuleBeTongSan.Enabled = flag;
            grcMacBeTong.Enabled = !flag;
        }

        public DataTable LoadMacBeTong()
        {
            Connect();
            DataTable loaithep = new DataTable();
            loaithep.Columns.Add("MacBeTongID", typeof(int));
            loaithep.Columns.Add("TenMacBeTong", typeof(string));
            try
            {
                cmd.CommandText = "SELECT * FROM MacBeTong";
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    DataRow dr = loaithep.NewRow();
                    dr["MacBeTongID"] = int.Parse(reader["MacBeTongID"].ToString());
                    dr["TenMacBeTong"] = reader["TenMacBeTong"].ToString();
                    loaithep.Rows.Add(dr);
                }
                return loaithep;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        public void InsertMacBeTong(MacBeTong thep)
        {
            try
            {
                cmd.CommandText = "INSERT INTO MacBeTong(TenMacBeTong,BeTongSan,BeTongCot,ModuleDanHoiBeTongSan,ModuleDanHoiBeTongCot) VALUES('" + thep.TenMacBeTong + "','" + thep.BeTongSan + "','" + thep.BeTongCot + "','" + thep.ModuleDanHoiBeTongSan + "','" + thep.ModuleDanHoiBeTongCot + "')";
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public MacBeTong FindMacBeTong(int t)
        {
            try
            {
                Connect();
                MacBeTong the = new MacBeTong();
                cmd.CommandText = "SELECT * FROM MacBeTong WHERE MacBeTongID=" + t;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    the.MacBeTongID = int.Parse(reader["MacBeTongID"].ToString());
                    the.TenMacBeTong = reader["TenMacBeTong"].ToString();
                    the.BeTongSan = float.Parse(reader["BeTongSan"].ToString());
                    the.BeTongCot = float.Parse(reader["BeTongCot"].ToString());
                    the.ModuleDanHoiBeTongSan = float.Parse(reader["ModuleDanHoiBeTongSan"].ToString());
                    the.ModuleDanHoiBeTongCot = float.Parse(reader["ModuleDanHoiBeTongCot"].ToString());
                }


                return the;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void DeleteMacBeTong(int id)
        {
            try
            {
                cmd.CommandText = "DELETE FROM MacBeTong WHERE MacBeTongID = " + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void Delete(MacBeTong m)
        {

            try
            {
                cmd.CommandText = "DELETE FROM KetQua WHERE MacBeTongID = " + m.MacBeTongID;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        public KetQua FindKetQua(MacBeTong m)
        {
            try
            {
                Connect();
                KetQua ketqua = new KetQua();
                cmd.CommandText = "SELECT * FROM KetQua WHERE MacBeTongID = " + m.MacBeTongID;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ketqua.KetQuaID = int.Parse(reader["KetQuaID"].ToString());
                }
                return ketqua;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        #endregion

        #region Mac thep san

        public void ActiveButton3(bool them, bool luu, bool xoa, bool huy)
        {
            btnThem3.Enabled = them;
            btnLuu3.Enabled = luu;
            btnXoa3.Enabled = xoa;
            btnHuy3.Enabled = huy;

        }

        public void CleanText3()
        {
            txtTenMacThepSan.Text = "";
            txtCotThepSan.Text = "";
            txtModuleCotThepSan.Text = "";
        }

        public void ActiveText3(bool flag)
        {
            txtTenMacThepSan.Enabled = flag;
            txtCotThepSan.Enabled = flag;
            txtModuleCotThepSan.Enabled = flag;
            grcMacThepSan.Enabled = !flag;
        }

        public DataTable LoadMacThepSan()
        {
            Connect();
            DataTable loaithep = new DataTable();
            loaithep.Columns.Add("MacThepSanID", typeof(int));
            loaithep.Columns.Add("TenMacThepSan", typeof(string));
            try
            {
                cmd.CommandText = "SELECT * FROM MacThepSan";
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    DataRow dr = loaithep.NewRow();
                    dr["MacThepSanID"] = int.Parse(reader["MacThepSanID"].ToString());
                    dr["TenMacThepSan"] = reader["TenMacThepSan"].ToString();
                    loaithep.Rows.Add(dr);
                }
                return loaithep;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        public void InsertMacThepSan(MacThepSan thep)
        {
            try
            {
                cmd.CommandText = "INSERT INTO MacThepSan(TenMacThepSan,CuongDoCotThepSan,ModuleDanHoiCotThepSan) VALUES('" + thep.TenMacThepSan + "','" + thep.CotThepSan + "','" + thep.ModuleDanHoiCotThepSan + "' )";
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public MacThepSan FindMacThepSan(int t)
        {
            try
            {
                Connect();
                MacThepSan thep = new MacThepSan();
                cmd.CommandText = "SELECT * FROM MacThepSan WHERE MacThepSanID=" + t;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    thep.MacThepSanID = int.Parse(reader["MacThepSanID"].ToString());
                    thep.TenMacThepSan = reader["TenMacThepSan"].ToString();
                    thep.CotThepSan = float.Parse(reader["CuongDoCotThepSan"].ToString());
                    thep.ModuleDanHoiCotThepSan = float.Parse(reader["ModuleDanHoiCotThepSan"].ToString());
                }


                return thep;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void DeleteMacThepSan(int id)
        {
            try
            {
                cmd.CommandText = "DELETE FROM MacThepSan WHERE MacThepSanID = " + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void Delete(MacThepSan m)
        {

            try
            {
                cmd.CommandText = "DELETE FROM KetQua WHERE MacThepSanID = " + m.MacThepSanID;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        public KetQua FindKetQua(MacThepSan m)
        {
            try
            {
                Connect();
                KetQua ketqua = new KetQua();
                cmd.CommandText = "SELECT * FROM KetQua WHERE MacThepSanID = " + m.MacThepSanID;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ketqua.KetQuaID = int.Parse(reader["KetQuaID"].ToString());
                }
                return ketqua;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        #endregion

        #region Mac bu long

        public void ActiveButton4(bool them, bool luu, bool xoa, bool huy)
        {
            btnThem4.Enabled = them;
            btnLuu4.Enabled = luu;
            btnXoa4.Enabled = xoa;
            btnHuy4.Enabled = huy;

        }

        public void CleanText4()
        {
            txtTenMacBuLong.Text = "";
            txtBuLong.Text = "";
            //txtModuleBuLong.Text = "";
        }

        public void ActiveText4(bool flag)
        {
            txtTenMacBuLong.Enabled = flag;
            txtBuLong.Enabled = flag;
            //txtModuleBuLong.Enabled = flag;
            grcMacBuLong.Enabled = !flag;
        }

        public DataTable LoadMacBuLong()
        {
            Connect();
            DataTable bulong = new DataTable();
            bulong.Columns.Add("MacBuLongID", typeof(int));
            bulong.Columns.Add("TenMacBuLong", typeof(string));
            try
            {
                cmd.CommandText = "SELECT * FROM MacBuLong";
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    DataRow dr = bulong.NewRow();
                    dr["MacBuLongID"] = int.Parse(reader["MacBuLongID"].ToString());
                    dr["TenMacBuLong"] = reader["TenMacBuLong"].ToString();
                    bulong.Rows.Add(dr);
                }
                return bulong;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        public void InsertMacBuLong(MacBuLong thep)
        {
            try
            {
                cmd.CommandText = "INSERT INTO MacBuLong(TenMacBuLong,BuLong) VALUES('" + thep.TenMacBuLong + "','" + thep.BuLong + "' )";
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public MacBuLong FindMacBuLong(int t)
        {
            try
            {
                Connect();
                MacBuLong thep = new MacBuLong();
                cmd.CommandText = "SELECT * FROM MacBuLong WHERE MacBuLongID=" + t;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    thep.MacBuLongID = int.Parse(reader["MacBuLongID"].ToString());
                    thep.TenMacBuLong = reader["TenMacBuLong"].ToString();
                    thep.BuLong = float.Parse(reader["BuLong"].ToString());
                    //thep.ModuleDanHoiBuLong = float.Parse(reader["ModuleDanHoiBuLong"].ToString());
                }


                return thep;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void DeleteMacBuLong(int id)
        {
            try
            {
                cmd.CommandText = "DELETE FROM MacBuLong WHERE MacBuLongID = " + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void Delete(MacBuLong m)
        {

            try
            {
                cmd.CommandText = "DELETE FROM KetQua WHERE MacBuLongID = " + m.MacBuLongID;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        public KetQua FindKetQua(MacBuLong m)
        {
            try
            {
                Connect();
                KetQua ketqua = new KetQua();
                cmd.CommandText = "SELECT * FROM KetQua WHERE MacBuLongID = " + m.MacBuLongID;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ketqua.KetQuaID = int.Parse(reader["KetQuaID"].ToString());
                }
                return ketqua;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        #endregion

        #endregion

        #region Form events

        private void FrmDacTrungVatLieu_Load(object sender, EventArgs e)
        {
            Connect();

            grcMacBuLong.DataSource = LoadMacBuLong();
            grcMacThep.DataSource = LoadMacThep();
            grcMacBeTong.DataSource = LoadMacBeTong();
            grcMacThepSan.DataSource = LoadMacThepSan();

            ActiveButton1(true, false, false, false);
            ActiveButton1(true, false, false, false);
            ActiveButton2(true, false, false, false);
            ActiveButton3(true, false, false, false);
            ActiveButton4(true, false, false, false);

            ActiveText1(false);
            ActiveText2(false);
            ActiveText3(false);
            ActiveText4(false); 
        }

        #region Mac thep ket cau

        private void btnThem1_Click(object sender, EventArgs e)
        {
            Connect();
            CleanText1();
            ActiveText1(true);
            grcMacThep.Enabled = false;
            ActiveButton1(false, true, false, true);
            txtTenMacThep.Focus();
        }

        private void btnLuu1_Click(object sender, EventArgs e)
        {
            Connect();
            //Kiem tra o text rong
            if (txtTenMacThep.Text == "") { txtTenMacThep.ErrorText = "Nhập Tên Mác Thép"; return; }
            if (txtBungDam.Text == "") { txtBungDam.ErrorText = "Nhập cường độ tính toán cho bụng dầm"; return; }
            if (txtCanhDam.Text == "") { txtCanhDam.ErrorText = "Nhập cường độ tính toán cho cánh dầm"; return; }
            if (txtBungCot.Text == "") { txtBungCot.ErrorText = "Nhập cường độ tính toán cho bụng cột"; return; }
            if (txtCanhCot.Text == "") { txtCanhCot.ErrorText = "Nhập cường độ tính toán cho cánh cột"; return; }
            if (txtSuonGoi.Text == "") { txtSuonGoi.ErrorText = "Nhập cường độ tính toán cho sườn gối"; return; }
            if (txtTamDem.Text == "") { txtTamDem.ErrorText = "Nhập cường độ tính toán cho tấm đệm"; return; }
            if (txtModuleThepKetCau.Text == "") { txtBungDam.ErrorText = "Nhập module đàn hồi cho mác thép kết cấu"; return; }

            //Khai bao 1 bien rong
            MacThepKetCau thep = new MacThepKetCau();


            //gan du lieu cho bien rong tu cac o text
            thep.TenMacThep = txtTenMacThep.Text.Trim();
            thep.BungDam = float.Parse(txtBungDam.Text.Trim());
            thep.CanhDam = float.Parse(txtCanhDam.Text.Trim());
            thep.BungCot = float.Parse(txtBungCot.Text.Trim());
            thep.CanhCot = float.Parse(txtCanhCot.Text.Trim());
            thep.SuonGoi = float.Parse(txtSuonGoi.Text.Trim());
            thep.TamDem = float.Parse(txtTamDem.Text.Trim());
            thep.ModuleDanHoiThepKetCau = float.Parse(txtModuleThepKetCau.Text.Trim());

            //Them vao database
            InsertMacThep(thep);

            CleanText1();
            ActiveButton1(true, false, false, false);
            grcMacThep.Enabled = true;
            ActiveText1(false);
            grcMacThep.DataSource = LoadMacThep();
        }

        private void btnXoa1_Click(object sender, EventArgs e)
        {
            Connect();
            if (MacThepID != "")
            {
                MacThepKetCau thep = FindMacThep(int.Parse(MacThepID));
                if (thep != null)
                {
                    KetQua k = FindKetQua(thep);
                    if (k.KetQuaID != 0)
                    {
                        DialogResult rs = MessageBox.Show("Thông số này đã được dùng để tính toán, nếu bạn xóa thông số này, chương trình sẽ xóa cả kết quả dùng thông số này để tính toán!! Bạn có chắc muốn xóa??", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (rs == DialogResult.Yes)
                        {
                            DeleteMacThep(thep.MacThepKetCauID);
                            Delete(thep);
                            
                        }
                    }
                    else { 
                        DeleteMacThep(thep.MacThepKetCauID);
                    }

                    CleanText1();
                    grcMacThep.DataSource = LoadMacThep();
                    ActiveText1(false);
                    ActiveButton1(true, false, false, false);

                }
                else
                    MessageBox.Show("Loại thép ko tồn tại");
            }
        }

        private void btnHuy1_Click(object sender, EventArgs e)
        {
            Connect();
            CleanText1();
            ActiveButton1(true, false, false, false);
            grcMacThep.Enabled = true;
            ActiveText1(false);
            MacThepID = "";
        }

        private void grvMacThep_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Connect();
            if (grvMacThep.SelectedRowsCount > 0)
            {

                MacThepKetCau thep = FindMacThep((int)grvMacThep.GetRowCellValue(grvMacThep.GetSelectedRows()[0], "MacThepKetCauID"));

                if (thep != null)
                {
                    MacThepID = thep.MacThepKetCauID.ToString();
                    txtTenMacThep.Text = thep.TenMacThep;
                    txtBungDam.Text = thep.BungDam.ToString();
                    txtCanhDam.Text = thep.CanhDam.ToString();
                    txtBungCot.Text = thep.BungCot.ToString();
                    txtCanhCot.Text = thep.CanhCot.ToString();
                    txtSuonGoi.Text = thep.SuonGoi.ToString();
                    txtTamDem.Text = thep.TamDem.ToString();
                    txtModuleThepKetCau.Text = thep.ModuleDanHoiThepKetCau.ToString();

                    ActiveButton1(true, false, true, true);
                }
            }
        }

        #endregion

        #region Mac be tong

        private void btnThem2_Click(object sender, EventArgs e)
        {
            Connect();
            CleanText2();
            ActiveText2(true);
            grcMacBeTong.Enabled = false;
            ActiveButton2(false, true, false, true);
            txtTenMacBeTong.Focus();
        }

        private void btnLuu2_Click(object sender, EventArgs e)
        {
            Connect();
            //Kiem tra o text rong
            if (txtTenMacBeTong.Text == "") { txtTenMacBeTong.ErrorText = "Nhập Tên Mác Bê Tông"; return; }

            //Khai bao 1 bien rong
            MacBeTong thep = new MacBeTong();


            //gan du lieu cho bien rong tu cac o text
            thep.TenMacBeTong = txtTenMacBeTong.Text.Trim();
            thep.BeTongSan = float.Parse(txtBeTongSan.Text.Trim());
            thep.BeTongCot = float.Parse(txtBeTongCot.Text.Trim());
            thep.ModuleDanHoiBeTongSan = float.Parse(txtModuleBeTongSan.Text.Trim());
            thep.ModuleDanHoiBeTongCot = float.Parse(txtModuleBeTongCot.Text.Trim());


            //Them vao database
            InsertMacBeTong(thep);

            CleanText2();
            ActiveButton2(true, false, false, false);
            grcMacBeTong.Enabled = true;
            ActiveText2(false);
            grcMacBeTong.DataSource = LoadMacBeTong();
        }

        private void btnXoa2_Click(object sender, EventArgs e)
        {
            Connect();
            if (MacBeTongID != "")
            {
                MacBeTong thep = FindMacBeTong(int.Parse(MacBeTongID));
                if (thep != null)
                {
                    KetQua k = FindKetQua(thep);
                    if (k.KetQuaID != 0)
                    {
                        DialogResult rs = MessageBox.Show("Thông số này đã được dùng để tính toán, nếu bạn xóa thông số này, chương trình sẽ xóa cả kết quả dùng thông số này để tính toán!! Bạn có chắc muốn xóa??", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (rs == DialogResult.Yes)
                        {
                            DeleteMacBeTong(thep.MacBeTongID);
                            Delete(thep);
                        }
                    }
                    else { 
                        DeleteMacBeTong(thep.MacBeTongID);
                    }
                    CleanText2();
                    grcMacBeTong.DataSource = LoadMacBeTong();
                    ActiveText2(false);
                    ActiveButton2(true, false, false, false);
                }
                else
                    MessageBox.Show("Loại thép ko tồn tại");
            }
        }

        private void btnHuy2_Click(object sender, EventArgs e)
        {
            Connect();
            CleanText2();
            ActiveButton2(true, false, false, false);
            grcMacThepSan.Enabled = true;
            ActiveText2(false);
            MacThepSanID = "";
        }

        private void grvMacBeTong_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Connect();
            if (grvMacBeTong.SelectedRowsCount > 0)
            {

                MacBeTong thep = FindMacBeTong((int)grvMacBeTong.GetRowCellValue(grvMacBeTong.GetSelectedRows()[0], "MacBeTongID"));

                if (thep != null)
                {
                    MacBeTongID = thep.MacBeTongID.ToString();
                    txtTenMacBeTong.Text = thep.TenMacBeTong;
                    txtBeTongSan.Text = thep.BeTongSan.ToString();
                    txtBeTongCot.Text = thep.BeTongCot.ToString();
                    txtModuleBeTongSan.Text = thep.ModuleDanHoiBeTongSan.ToString();
                    txtModuleBeTongCot.Text = thep.ModuleDanHoiBeTongCot.ToString();

                    ActiveButton2(true, false, true, true);
                }
            }
        }

        #endregion

        #region Mac thep san

        private void btnThem3_Click(object sender, EventArgs e)
        {
            Connect();
            CleanText3();
            ActiveText3(true);
            grcMacThepSan.Enabled = false;
            ActiveButton3(false, true, false, true);
            txtTenMacThepSan.Focus();
        }

        private void btnLuu3_Click(object sender, EventArgs e)
        {
            Connect();
            //Kiem tra o text rong
            if (txtTenMacThepSan.Text == "") { txtTenMacThepSan.ErrorText = "Nhập Tên Mác Thép Sàn"; return; }

            //Khai bao 1 bien rong
            MacThepSan thep = new MacThepSan();


            //gan du lieu cho bien rong tu cac o text
            thep.TenMacThepSan = txtTenMacThepSan.Text.Trim();
            thep.CotThepSan = float.Parse(txtCotThepSan.Text.Trim());
            thep.ModuleDanHoiCotThepSan = float.Parse(txtModuleCotThepSan.Text.Trim());

            //Them vao database
            InsertMacThepSan(thep);

            CleanText3();
            ActiveButton3(true, false, false, false);
            grcMacThepSan.Enabled = true;
            ActiveText3(false);
            grcMacThepSan.DataSource = LoadMacThepSan();
        }

        private void btnXoa3_Click(object sender, EventArgs e)
        {
            Connect();
            if (MacThepSanID != "")
            {
                MacThepSan thep = FindMacThepSan(int.Parse(MacThepSanID));
                if (thep != null)
                {
                    KetQua k = FindKetQua(thep);
                    if (k.KetQuaID != 0)
                    {
                        DialogResult rs = MessageBox.Show("Thông số này đã được dùng để tính toán, nếu bạn xóa thông số này, chương trình sẽ xóa cả kết quả dùng thông số này để tính toán!! Bạn có chắc muốn xóa??", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (rs == DialogResult.Yes)
                        {
                            DeleteMacThepSan(thep.MacThepSanID);
                            Delete(thep);
                        }
                    }
                    else { 
                        DeleteMacThepSan(thep.MacThepSanID);
                    }
                    CleanText3();
                    grcMacThepSan.DataSource = LoadMacThepSan();
                    ActiveText3(false);
                    ActiveButton3(true, false, false, false);
                }
                else
                    MessageBox.Show("Loại thép ko tồn tại");
            }
        }

        private void btnHuy3_Click(object sender, EventArgs e)
        {
            Connect();
            CleanText3();
            ActiveButton3(true, false, false, false);
            grcMacBeTong.Enabled = true;
            ActiveText3(false);
            MacBeTongID = "";
        }

        private void grvMacThepSan_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Connect();
            if (grvMacThepSan.SelectedRowsCount > 0)
            {

                MacThepSan thep = FindMacThepSan((int)grvMacThepSan.GetRowCellValue(grvMacThepSan.GetSelectedRows()[0], "MacThepSanID"));

                if (thep != null)
                {
                    MacThepSanID = thep.MacThepSanID.ToString();
                    txtTenMacThepSan.Text = thep.TenMacThepSan;
                    txtCotThepSan.Text = thep.CotThepSan.ToString();
                    txtModuleCotThepSan.Text = thep.ModuleDanHoiCotThepSan.ToString();
                    ActiveButton3(true, false, true, true);
                }
            }
        }

        #endregion

        #region Mac bu long

        private void btnThem4_Click(object sender, EventArgs e)
        {
            Connect();
            CleanText4();
            ActiveText4(true);
            grcMacBuLong.Enabled = false;
            ActiveButton4(false, true, false, true);
            txtTenMacBuLong.Focus();
        }

        private void btnLuu4_Click(object sender, EventArgs e)
        {
            Connect();
            //Kiem tra o text rong
            if (txtTenMacBuLong.Text == "") { txtTenMacBuLong.ErrorText = "Nhập Tên Mác Bu lông"; return; }
            if (txtBuLong.Text == "") { txtBuLong.ErrorText = "Nhập cường độ tính toán cho bu lông"; return; }

            //Khai bao 1 bien rong
            MacBuLong thep = new MacBuLong();


            //gan du lieu cho bien rong tu cac o text
            thep.TenMacBuLong = txtTenMacBuLong.Text.Trim();
            thep.BuLong = float.Parse(txtBuLong.Text.Trim());
            //thep.ModuleDanHoiBuLong = float.Parse(txtModuleBuLong.Text.Trim());

            //Them vao database
            InsertMacBuLong(thep);

            CleanText4();
            ActiveButton4(true, false, false, false);
            grcMacBuLong.Enabled = true;
            ActiveText4(false);
            grcMacBuLong.DataSource = LoadMacBuLong();
        }

        private void btnXoa4_Click(object sender, EventArgs e)
        {
            Connect();
            if (MacBuLongID != "")
            {
                MacBuLong thep = FindMacBuLong(int.Parse(MacBuLongID));
                if (thep != null)
                {
                    KetQua k = FindKetQua(thep);
                    if (k.KetQuaID != 0)
                    {
                        DialogResult rs = MessageBox.Show("Thông số này đã được dùng để tính toán, nếu bạn xóa thông số này, chương trình sẽ xóa cả kết quả dùng thông số này để tính toán!! Bạn có chắc muốn xóa??", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (rs == DialogResult.Yes)
                        {
                            DeleteMacBuLong(thep.MacBuLongID);
                            Delete(thep);
                            
                        }
                    }
                    else { 
                        DeleteMacBuLong(thep.MacBuLongID);
                    }

                    CleanText4();
                    grcMacBuLong.DataSource = LoadMacBuLong();
                    ActiveText4(false);
                    ActiveButton4(true, false, false, false);
                }
                else
                    MessageBox.Show("Mác bulong ko tồn tại");
            }
        }

        private void btnHuy4_Click(object sender, EventArgs e)
        {
            Connect();
            CleanText4();
            ActiveButton4(true, false, false, false);
            grcMacBuLong.Enabled = true;
            ActiveText4(false);
            MacBuLongID = "";
        }

        private void grvMacBuLong_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Connect();
            if (grvMacBuLong.SelectedRowsCount > 0)
            {

                MacBuLong thep = FindMacBuLong((int)grvMacBuLong.GetRowCellValue(grvMacBuLong.GetSelectedRows()[0], "MacBuLongID"));

                if (thep != null)
                {
                    MacBuLongID = thep.MacBuLongID.ToString();
                    txtTenMacBuLong.Text = thep.TenMacBuLong;
                    txtBuLong.Text = thep.BuLong.ToString();
                    //txtModuleBuLong.Text = thep.ModuleDanHoiBuLong.ToString();
                    ActiveButton4(true, false, true, true);
                }
            }
        }

        #endregion

        

        #endregion

    }
}
